package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.product.pojo.dto.SettleAccountsDTO;
import com.xbongbong.pro.product.pojo.dto.SettleAccountsListDTO;
import com.xbongbong.pro.product.pojo.vo.SettleAccountsListVO;
import com.xbongbong.pro.product.pojo.vo.SettleAccountsVO;
import com.xbongbong.saas.service.SettleAccountsService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 结账控制层<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author ruansicheng
 * @version v1.0
 * @since 2021-11-18 10:20
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/settleAccounts")
public class SettleAccountsController {

    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private SettleAccountsService settleAccountsService;



    /**
     * 获取上一次结账日期，系统启用日期
     * @param baseDTO
     * @param br
     * @return java.lang.String
     * @author 阮思程
     * @date 2021-11-18
     */
    @RequestMapping(value = "/getDate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCheckDate(@RequestBody @Valid BaseDTO baseDTO, BindingResult br){
        XbbResponse<SettleAccountsVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SettleAccountsVO settleAccountsVO = settleAccountsService.getCheckDate(baseDTO);
                response = new XbbResponse<>(settleAccountsVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 新增结账接口
     * @param settleAccountsDTO
     * @param br
     * @return java.lang.String
     * @author 阮思程
     * @date 2021-11-18
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addSettleAccounts(@RequestBody @Valid SettleAccountsDTO settleAccountsDTO, BindingResult br){
        XbbResponse<SettleAccountsVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(settleAccountsDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                SettleAccountsVO settleAccountsVO = settleAccountsService.addSettleAccounts(settleAccountsDTO);
                response = new XbbResponse<>(settleAccountsVO);
            } catch (XbbException e) {
                paasRedisHelper.removeValue(RedisPrefixConstant.SETTLE_ACCOUNTS, settleAccountsDTO.getCorpid());
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
}

    /**
     * 新增反结账接口
     * @param settleAccountsDTO
     * @param br
     * @return java.lang.String
     * @author 阮思程
     * @date 2021-11-18
     */
    @RequestMapping(value = "/antiSettlement", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String antiSettlement(@RequestBody @Valid SettleAccountsDTO settleAccountsDTO, BindingResult br){
        XbbResponse<SettleAccountsVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(settleAccountsDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
                SettleAccountsVO settleAccountsVO = settleAccountsService.antiSettlement(settleAccountsDTO);
                response = new XbbResponse<>(settleAccountsVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 结账&反结账列表接口
     * @param settleAccountsListDTO
     * @param br
     * @return java.lang.String
     * @author 阮思程
     * @date 2021-11-24
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSettleAccountsList(@RequestBody @Valid SettleAccountsListDTO settleAccountsListDTO, BindingResult br) throws Exception {
        XbbResponse<SettleAccountsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SettleAccountsListVO settleAccountsListVO = settleAccountsService.getSettleAccountsList(settleAccountsListDTO);
                response = new XbbResponse<>(settleAccountsListVO);
            } catch (XbbException xe) {
                response = new XbbResponse<>(xe);
            }
        }
        return JSON.toJSONString(response);
    }
}
